﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _39_ListBoxControl.Models;
using System.Text;


namespace _39_ListBoxControl.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {
            SampleDBContext db = new SampleDBContext();
            List<SelectListItem> selectListItem = new List<SelectListItem>();
            foreach (var city in db.Cities)
            {
                SelectListItem cityitem = new SelectListItem
                {
                    Text = city.Name,
                    Value = city.ID.ToString(),
                    Selected = city.isSelected
                };
                selectListItem.Add(cityitem);
            }

            CitiesViewModelClass citiesViewModelClass = new CitiesViewModelClass();
            citiesViewModelClass.cities = selectListItem;
            return View(citiesViewModelClass);
        }


        public string Index(IEnumerable<string> selectedCities)
        {
            if (selectedCities == null)
            {
                return "You have not selected any city";
            }
            else
            {
                StringBuilder str = new StringBuilder();
                str.Append("You hv selected="+ string.Join(",",selectedCities));
                return str.ToString();
            }

        }
    }
}
